#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

// 需要长时间计算的函数
int sqr_func (int n) {
    int result = n * n;

    sleep(1);

    return result;
}


int main(int argc, char const *argv[])
{
    int nums[5] = {1, 2, 3, 4, 5};
    int sqrs[5];

    struct timeval start, end;
    gettimeofday(&start, NULL);

    for (int i = 0; i < 5; i++) {
        sqrs[i] = sqr_func(nums[i]);
    }
    gettimeofday(&end, NULL);
    printf("计算时间: %ld \n", end.tv_sec - start.tv_sec);

    int sum = 0;
    for (int i = 0; i < 5; i++) {
        sum += sqrs[i];
    }
    printf("sum: %d\n", sum);

    
    return 0;
}